<template>
  <div id="active-container">
    <el-card>
      <template #header>
        <div class="clearfix">
          <span>
            <el-breadcrumb separator-class="el-icon-arrow-right">
              <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item>百叶管理</el-breadcrumb-item>
              <el-breadcrumb-item>百叶积分</el-breadcrumb-item>
            </el-breadcrumb></span
          >
        </div>
      </template>
      <div>
        <el-button
          size="small"
          icon="el-icon-download"
          type="success"
          @click="exportActive"
          >导出</el-button
        >
        <el-input
          placeholder="请输入内容"
          prefix-icon="el-icon-search"
          v-model="keyword"
          size="small"
          style="width: 200px; margin-left: 15px"
        >
        </el-input>
        <el-select
          v-model="collageNum"
          size="small"
          clearable
          placeholder="请选择学院"
          style="width: 150px; margin-left: 15px"
          @change="collageChange"
        >
          <el-option
            v-for="item in collages"
            :key="item.collageNum"
            :label="item.collageName"
            :value="item.collageNum"
          >
          </el-option>
        </el-select>
        <el-select
          v-if="majors.length > 0"
          v-model="majorNum"
          size="small"
          clearable
          placeholder="请选择专业"
          style="width: 150px; margin-left: 15px"
          @change="majorChange"
        >
          <el-option
            v-for="item in majors"
            :key="item.majorNum"
            :label="item.majorName"
            :value="item.majorNum"
          >
          </el-option>
        </el-select>
        <el-select
          v-if="calzzs.length > 0"
          v-model="classNum"
          size="small"
          clearable
          placeholder="请选择班级"
          style="width: 150px; margin-left: 15px"
          @change="classChange"
        >
          <el-option
            v-for="item in calzzs"
            :key="item.classNum"
            :label="item.className"
            :value="item.classNum"
          >
          </el-option>
        </el-select>
      </div>
      <el-table
        :data="scores"
        v-loading="loading"
        :height="height"
        :stripe="true"
        style="width: 100%; top: 15px"
      >
        <el-table-column label="学号" width="120" fixed="left" prop="studentId">
        </el-table-column>

        <el-table-column
          prop="studentName"
          width="80"
          label="姓名"
          fixed="left"
        >
        </el-table-column>
        <el-table-column
          label="学院"
          prop="collageName"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="专业"
          prop="majorName"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="班级"
          prop="className"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="boxue" width="80" label="博学分数">
        </el-table-column>
        <el-table-column prop="duxing" width="80" label="笃行分数">
        </el-table-column>
        <el-table-column prop="jingmei" width="80" label="尽美分数">
        </el-table-column>
        <el-table-column prop="mingde" width="80" label="明德分数">
        </el-table-column>
        <el-table-column
          prop="updateTime"
          label="最近更新"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column width="80" label="是否合格" fixed="right">
          <template #default="scope">
            <el-tag v-if="scope.row.qualified == 0" type="success">合格</el-tag>
            <el-tag type="danger" v-else>不合格</el-tag>
          </template>
        </el-table-column>
      </el-table>

      <div style="margin-top: 30px">
        <el-pagination
          @size-change="sizeChange"
          @current-change="pageChange"
          :current-page="page"
          :page-sizes="[15, 30, 50]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :disabled="loading"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts">
import { reactive, toRefs, watch } from "vue";
import { BaiyeService, Score } from "@/api/baiye";
import Code from "@/consts/common";
import { UserService, Collageinfo, Major, Clazz } from "@/api/user";

export default {
  name: "Active",
  setup() {
    const scores: Score[] = [];
    const collages: Collageinfo[] = [];
    const majors: Major[] = [];
    const calzzs: Clazz[] = [];
    let data = reactive({
      height: document.documentElement.clientHeight - 280,
      keyword: "",
      loading: false,
      total: 0,
      page: 1,
      size: 15,
      scores: scores,
      collageNum: "",
      collages: collages,
      majorNum: "",
      majors: majors,
      classNum: "",
      calzzs: calzzs,
      exportActive() {
        BaiyeService.exportScore({
          keyword: data.keyword,
          collageNum: data.collageNum,
          majorNum: data.majorNum,
          classNum: data.classNum,
        }).then((res) => {});
      },
      pageChange: (page: number) => {
        data.page = page;
        listScore(
          data.page,
          data.size,
          data.keyword,
          data.collageNum,
          data.majorNum,
          data.classNum
        );
      },

      sizeChange: (size: number) => {
        data.size = size;
        listScore(
          data.page,
          data.size,
          data.keyword,
          data.collageNum,
          data.majorNum,
          data.classNum
        );
      },
      collageChange(collageNum: string) {
        data.collageNum = collageNum;
        if (collageNum == "") {
          data.majorNum = "";
          data.majors = [];
          data.classNum = "";
          data.calzzs = [];
        } else {
          UserService.getCollageinfoById(data.collageNum).then((res) => {
            if (res.code == Code.CODE_SUCCESS) {
              data.majors = res.data;
            }
          });
        }
        listScore(data.page, data.size, data.keyword, data.collageNum);
      },
      majorChange(majorNum: string) {
        data.majorNum = majorNum;
        if (majorNum == "") {
          data.classNum = "";
          data.calzzs = [];
        } else {
          UserService.getMajorinfoClass(data.majorNum).then((res) => {
            if (res.code == Code.CODE_SUCCESS) {
              data.calzzs = res.data;
            }
          });
        }
        listScore(
          data.page,
          data.size,
          data.keyword,
          data.collageNum,
          data.majorNum
        );
      },
      classChange(classNum: string) {
        data.classNum = classNum;
        listScore(
          data.page,
          data.size,
          data.keyword,
          data.collageNum,
          data.majorNum,
          data.classNum
        );
      },
    });
    const listScore = (
      page: number = 1,
      size: number = 15,
      keyword: string = "",
      collageNum: string = "",
      majorNum: string = "",
      classNum: string = ""
    ) => {
      data.loading = true;
      BaiyeService.listScore({
        page,
        size,
        keyword,
        collageNum,
        majorNum,
        classNum,
      }).then((res) => {
        if (res.code == Code.CODE_SUCCESS) {
          data.scores = res.data.data;
          data.total = res.data.total;
        }
        data.loading = false;
      });
    };
    const getCollageinfo = () => {
      UserService.getCollageinfo().then((res) => {
        if (res.code == Code.CODE_SUCCESS) {
          data.collages = res.data;
        }
      });
    };
    listScore();
    getCollageinfo();
    watch(
      () => data.keyword,
      (newValue, oldVlaue) => {
        // @ts-ignore
        listScore(data.page, data.size, data.keyword);
      }
    );

    const getHeight = () => {
      data.height = document.documentElement.clientHeight - 280;
    };
    window.addEventListener("resize", getHeight, false);
    return {
      ...toRefs(data),
    };
  },
};
</script>

<style lang="less" scoped>
</style>

<style lang="less">
.active-dialog {
  .el-card__body {
    padding: 5px 15px !important;
  }
  .el-dialog__body {
    padding: 20px 20px;
  }
}
</style>
